package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.ModuleDao;
import com.itheima.domain.Module;
import com.itheima.service.ModuleService;
import com.itheima.utils.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ModuleServiceImpl implements ModuleService {
    @Override
    public PageInfo<Module> findByPage(int currentPage, int pageSize) {
        PageInfo<Module> page = null;
        SqlSession sqlSession = null;
        try {
            //1 通过MybatisUtil工厂类获取SqlSession对象
            sqlSession = MybatisUtil.getSqlSession();
            //2 通过MybatisUtil工厂类获取dao接口的代理对象
            ModuleDao mapper = MybatisUtil.getMapper(sqlSession, ModuleDao.class);
            //3 执行操作释放资源
            //3.1 在查询所有之前设置分页参数
            PageHelper.startPage(currentPage,pageSize);
            List<Module> list = mapper.findAll();
             page = new PageInfo<>(list);
            //MybatisUtil.commit(sqlSession);
        } finally {
            MybatisUtil.close(sqlSession);
        }
        return page;
    }

    @Override
    public void save(Module cept) {
        SqlSession sqlSession=null;
        try {
             sqlSession = MybatisUtil.getSqlSession();
            ModuleDao mapper = MybatisUtil.getMapper(sqlSession, ModuleDao.class);
            //生成一个唯一的id，保存到cept
            String id = UUID.randomUUID().toString().replace("-", "");
            cept.setId(id);
            mapper.save(cept);
            MybatisUtil.commit(sqlSession);

        } finally {
            MybatisUtil.close(sqlSession);
        }
    }

    @Override
    public Module findById(String id) {
        Module cept = null;
        SqlSession sqlSession=null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            ModuleDao mapper = MybatisUtil.getMapper(sqlSession, ModuleDao.class);

            cept =  mapper.findById(id);
            MybatisUtil.commit(sqlSession);

        } finally {
            MybatisUtil.close(sqlSession);
        }
        return cept;
    }

    @Override
    public void update(Module cept) {
        SqlSession sqlSession=null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            ModuleDao mapper = MybatisUtil.getMapper(sqlSession, ModuleDao.class);

            mapper.update(cept);
            MybatisUtil.commit(sqlSession);

        } finally {
            MybatisUtil.close(sqlSession);
        }
    }

    @Override
    public void delete(String[] ids) {
        SqlSession sqlSession=null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            ModuleDao mapper = MybatisUtil.getMapper(sqlSession, ModuleDao.class);

            Stream.of(ids).forEach(mapper::delete);
            MybatisUtil.commit(sqlSession);

        } finally {
            MybatisUtil.close(sqlSession);
        }
    }

    @Override
    public List<Module> findAll() {
       List<Module>  list = null;
        SqlSession sqlSession=null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            ModuleDao mapper = MybatisUtil.getMapper(sqlSession, ModuleDao.class);

            list =  mapper.findAll();
            MybatisUtil.commit(sqlSession);

        } finally {
            MybatisUtil.close(sqlSession);
        }
        return list;
    }

    @Override
    public List<Map<String, Object>> findModulesByRoleId(String roleId) {
        List<Map<String, Object>>  list = null;
        SqlSession sqlSession=null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            ModuleDao mapper = MybatisUtil.getMapper(sqlSession, ModuleDao.class);

            list =  mapper.findModulesByRoleId(roleId);
            MybatisUtil.commit(sqlSession);

        } finally {
            MybatisUtil.close(sqlSession);
        }
        return list;
    }

    @Override
    public List<Module> findMenuByUserId(String id) {
        List<Module>  list = null;
        SqlSession sqlSession=null;
        try {
            //1 获取SqlSession对象
            sqlSession = MybatisUtil.getSqlSession();
            //2 获取dao对应的代理对象
            ModuleDao mapper = MybatisUtil.getMapper(sqlSession, ModuleDao.class);
            //3 执行操作，集合中包含了一级菜单和二级菜单。
            List<Module> modules =  mapper.findMenuByUserId(id);
            //处理数据:将list集合中的module分出一级菜单和二级菜单，并且将二级菜单设置到一级菜单的children中
            //3.1 找出一级菜单
            list = modules.stream().filter(module ->
                    module.getCtype() == 0).collect(Collectors.toList());
            //3.2 遍历一级菜单，根据二级菜单的 parentId和一级菜单的id找出对应的二级菜单
            for (Module module : list) {
                List<Module> childern = modules.stream().filter(child ->
                        module.getId().equals(child.getParentId())).collect(Collectors.toList());
                //将二级菜单设置到一级菜单的children中
                module.setChildren(childern);
            }
            //4 返回结果并释放资源
            MybatisUtil.commit(sqlSession);

        } finally {
            //释放资源
            MybatisUtil.close(sqlSession);
        }
        return list;
    }

    @Override
    public List<String> findPermissionByUserId(String id) {
        List<String>  list = null;
        SqlSession sqlSession=null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            ModuleDao mapper = MybatisUtil.getMapper(sqlSession, ModuleDao.class);

            list =  mapper.findPermissionByUserId(id);
            MybatisUtil.commit(sqlSession);

        } finally {
            MybatisUtil.close(sqlSession);
        }
        return list;
    }
}
